<template>
  <div class="data-form file-form" v-loading.fullscreen.lock="loading">
    <div style="margin-bottom:10px">
      <upload-file-button size="medium" @change="fileChange"></upload-file-button>
    </div>
    <el-form label-width="auto" size="medium">
      <el-form-item label="文件名称">
        <el-input v-model="fileDataForm.fileName" :disabled="fileDataForm.file===null"></el-input>
      </el-form-item>
      <el-form-item label="文件前缀">
        <el-input v-model="fileDataForm.prefix"></el-input>
      </el-form-item>
      <el-form-item label="文件大小">
        <span>{{fileDataForm.size}}</span>
      </el-form-item>
      <el-form-item label="文件类型">
        <span>{{fileDataForm.mimeType}}</span>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button size="medium" type="primary" @click="saveFile">新增</el-button>
    </div>
  </div>
</template>

<script>
  import LogApi from 'services/logApi';

  import UploadFileButton from 'commonComponents/uploadFileButton';

  import FileDataForm from 'models/log/fileDataForm';

  export default {
    data() {
      return {
        loading: false,
        fileDataForm: new FileDataForm()
      };
    },

    methods: {
      fileChange(file) {
        this.fileDataForm.setFile(file);
      },

      saveFile() {
        let fileDataForm = this.fileDataForm;
        let code = fileDataForm.checkValid();
        if (code !== '') {
          let msg = this.i18n.getMsg(`log.fileMgr.error.${code}`);
          this.$message(msg);
          return;
        }

        this.addFile(fileDataForm);
      },

      addFile(fileDataForm) {
        this.loading = true;
        LogApi.uploadFile(fileDataForm).then(() => {
          this.loading = false;
          this.$alert('文件新增成功', '信息', {
            callback: () => {
              this.$emit('save-success');
            }
          });
        }).catch(() => {
          this.loading = false;
        });
      }
    },

    components: {
      UploadFileButton
    }
  };

</script>

<style lang="scss" scoped>
  @import '~viewStyle/dataForm';

</style>
